from requests.packages.urllib3.exceptions import InsecureRequestWarning

# !usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: lenovo by amidoufu
@file: amidoufu.py
@time: 2020/05/06
@function:
"""
import requests
import time
import os
import re
import sys
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

__headers_web = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}

class quanju:
    tujimingcheng = ""
    dir = ""
    dir2 = ""
    jishu = 0
    yanshi = 0
    tujimingcheng = ""


def chushi():
    page = 1
    zuozhelianjie = input("输入标签链接：")
        # res = requests.get(zuozhelianjie, headers=__headers_web, verify=False)
    while True:  # 一直循环，知道访问站点成功
        try:
            # 以下except都是用来捕获当requests请求出现异常时，
            # 通过捕获然后等待网络情况的变化，以此来保护程序的不间断运行
            res = requests.get(zuozhelianjie, headers=__headers_web, verify=False)
            # res = requests.get(w_url, headers=__headers_mobile, verify=False)
            # res.raise_for_status()
            break
        except requests.exceptions.HTTPError:
            print("HTTPError")
            time.sleep(10)
        except requests.exceptions.ConnectionError:
            print("ConnectionError")
            time.sleep(10)
        except requests.exceptions.ChunkedEncodingError:
            print("ChunkedEncodingError")
            time.sleep(10)
        except:
            print("Unfortunitely -- An Unknow Error Happened")
            time.sleep(10)
    html = res.text
    zuozhemingcheng_find = r'h4 align-content-center\">(.+?)</p>'
    zuozhemingcheng = re.findall(zuozhemingcheng_find, html)
    if not os.path.exists("./爱骚data/" + zuozhemingcheng[0]):
        os.makedirs("./爱骚data/" + zuozhemingcheng[0])
    quanju.dir2 = "爱骚data/" + zuozhemingcheng[0] + "/"

    tujiliebiao_find = r'href=\"https://www.f4mm.com/beauty/(.+?)\">'
    tujiliebiao = re.findall(tujiliebiao_find, html)

    mingchnegliebiao_find = r'item-link-text\">(.+?)</div>'
    mingchengliebiao = re.findall(mingchnegliebiao_find, html)


    if tujiliebiao != []:
        print("当前第" + str(page) +"页,共:" + str(len(tujiliebiao)) + "个图集")
        for tujiliebiao_jici in range(0, len(tujiliebiao)):
            tujilianjie = "https://www.f4mm.com/beauty/" + tujiliebiao[tujiliebiao_jici]
            quanju.tujimingcheng = mingchengliebiao[tujiliebiao_jici]
            print("当前获取第" + str(tujiliebiao_jici + 1) + "个图集丨图集名称:[" + quanju.tujimingcheng + "]丨图集链接:[" + tujilianjie + "]")
            time.sleep(10)
            huoqu_tupiandizhi(tujilianjie)

    while tujiliebiao != []:
        page += 1
        zuozhelianjie = zuozhelianjie + "/" + str(page)
        # res = requests.get(zuozhelianjie, headers=__headers_web, verify=False)
        while True:  # 一直循环，知道访问站点成功
            try:
                # 以下except都是用来捕获当requests请求出现异常时，
                # 通过捕获然后等待网络情况的变化，以此来保护程序的不间断运行
                res = requests.get(zuozhelianjie, headers=__headers_web, verify=False)
                # res = requests.get(w_url, headers=__headers_mobile, verify=False)
                # res.raise_for_status()
                break
            except requests.exceptions.HTTPError:
                print("HTTPError")
                time.sleep(10)
            except requests.exceptions.ConnectionError:
                print("ConnectionError")
                time.sleep(10)
            except requests.exceptions.ChunkedEncodingError:
                print("ChunkedEncodingError")
                time.sleep(10)
            except:
                print("Unfortunitely -- An Unknow Error Happened")
                time.sleep(10)
        html = res.text

        tujiliebiao_find = r'href=\"https://www.f4mm.com/beauty/(.+?)\">'
        tujiliebiao = re.findall(tujiliebiao_find, html)

        mingchnegliebiao_find = r'item-link-text\">(.+?)</div>'
        mingchengliebiao = re.findall(mingchnegliebiao_find, html)

        if tujiliebiao != []:
            print("当前第" + str(page) + "页,共:" + str(len(tujiliebiao)) + "个图集")
            for tujiliebiao_jici in range(0, len(tujiliebiao)):
                tujilianjie = "https://www.f4mm.com/beauty/" + tujiliebiao[tujiliebiao_jici]
                quanju.tujimingcheng = mingchengliebiao[tujiliebiao_jici]
                print("当前获取第" + str(tujiliebiao_jici + 1) + "个图集丨图集名称:[" + quanju.tujimingcheng + "]丨图集链接:[" + tujilianjie + "]")
                time.sleep(10)
                huoqu_tupiandizhi(tujilianjie)
    # for tujiliebiao_jici in range(0, len(tujiliebiao)):
    #     tujilianjie = "https://www.f4mm.com/beauty/" + tujiliebiao[tujiliebiao_jici]
    #     tujimingcheng = mingchengliebiao[tujiliebiao_jici]
    #     time.sleep(10)
    #     huoqu_id(tujilianjie, tujimingcheng)
    print("全部爬取完毕")




def huoqu_tupiandizhi(tujilianjie):
    #res = requests.get(tujilianjie, headers=__headers_web,verify=False)
    while True:  # 一直循环，知道访问站点成功
        try:
            # 以下except都是用来捕获当requests请求出现异常时，
            # 通过捕获然后等待网络情况的变化，以此来保护程序的不间断运行
            res = requests.get(tujilianjie, headers=__headers_web,verify=False)
            # res = requests.get(w_url, headers=__headers_mobile, verify=False)
            # res.raise_for_status()
            break
        except requests.exceptions.HTTPError:
            print("HTTPError")
            time.sleep(10)
        except requests.exceptions.ConnectionError:
            print("ConnectionError")
            time.sleep(10)
        except requests.exceptions.ChunkedEncodingError:
            print("ChunkedEncodingError")
            time.sleep(10)
        except:
            print("Unfortunitely -- An Unknow Error Happened")
            time.sleep(10)
    html = res.text

    tupiandizhi_liebiao_find = r'data-src=\"https://img.f4mm.com(.+?)\"'
    tupiandizhi_liebiao = re.findall(tupiandizhi_liebiao_find, html)

    tupianmingcheng_liebiao_find = r'title=\'(.+?)\''
    tupianmingcheng_liebiao = re.findall(tupianmingcheng_liebiao_find, html)

    shijian_find = r'<time>(.+?)</time>'
    shijian_yemian = re.findall(shijian_find, html)[0]
    shijian_shuzu = time.strptime(shijian_yemian, "%Y-%m-%d %H:%M:%S")
    shijian = time.strftime("%Y-%m-%d %H-%M-%S", shijian_shuzu)

    if not os.path.exists(quanju.dir2 + quanju.tujimingcheng + "_" + shijian ):
        os.makedirs(quanju.dir2 + quanju.tujimingcheng + "_" + shijian)
    quanju.dir = quanju.dir2 + quanju.tujimingcheng + "_" + shijian + "/"

    print("共:" + str(len(tupiandizhi_liebiao)) + "张图片")
    for tupiandizhi_liebiao_jici in range(0, len(tupiandizhi_liebiao)):
        tupianlianjie = "https://img.f4mm.com/" + tupiandizhi_liebiao[tupiandizhi_liebiao_jici]
        tupianmingcheng = tupianmingcheng_liebiao[tupiandizhi_liebiao_jici]
        xiazaitupian(tupianlianjie,tupianmingcheng,tupiandizhi_liebiao_jici)
        time.sleep(quanju.yanshi)
    
    


def xiazaitupian(xiazaidizhi, tupianmingcheng, tupiandizhi_liebiao_jici, retry=3):
    '''
    下载视频,显示进度

    :param index:       视频序号
    :param username:    用户名
    :param name:        视频名
    :param url:         视频地址
    :param retry:       重试次数

    :return:            None
    '''

    print(str(tupiandizhi_liebiao_jici + 1) + ">" + "\n正在下载图片:" + tupianmingcheng)
    photo_down = quanju.dir + tupianmingcheng + ".jpg"
    if os.path.exists(photo_down):
        print("图片已存在")
        quanju.yanshi = 0
        return
    else:
        print("图片正在下载")
        quanju.yanshi = 1
    try:
        #photo_response = requests.get(xiazaidizhi, headers=__headers_web, timeout=15)
        while True:  # 一直循环，知道访问站点成功
            try:
                # 以下except都是用来捕获当requests请求出现异常时，
                # 通过捕获然后等待网络情况的变化，以此来保护程序的不间断运行
                photo_response = requests.get(xiazaidizhi, headers=__headers_web, timeout=15)
                # res = requests.get(w_url, headers=__headers_mobile, verify=False)
                # res.raise_for_status()
                break
            except requests.exceptions.HTTPError:
                print( "HTTPError")
                time.sleep(3)
            except requests.exceptions.ConnectionError:
                print( "ConnectionError")
                time.sleep(3)
            except requests.exceptions.ChunkedEncodingError:
                print( "ChunkedEncodingError")
                time.sleep(3)
            except:
                print( "Unfortunitely -- An Unknow Error Happened")
                time.sleep(3)


        # 保存视频，显示下载进度
        if photo_response.status_code == 200:
            video_size = int(photo_response.headers['Content-Length'])
            if video_size/1024000 >= 10000:
                print("视频超过10G已跳过")
                print("\n")
                return
            else:
                with open(photo_down, 'wb') as f:
                    data_length = 0
                    for data in photo_response.iter_content(chunk_size=1024):
                        data_length += len(data)
                        f.write(data)
                        done = int(50 * data_length / video_size)
                        sys.stdout.write("\r下载进度: [%s%s]" % (
                            '█' * done, ' ' * (50 - done)))
                        sys.stdout.flush()
            print("\n")



        # 失败重试3次，超过放弃
        elif photo_response.status_code != 200 and retry:
            retry -= 1
            xiazaituji(xiazaidizhi, retry)
        else:
            return
    except Exception as e:
        print('download failed,', tupianmingcheng, e)
        return None


if __name__ == "__main__":
    chushi()